<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_db_link($link = '_db_link') {
	global $$link;
	global $adb;
	
	if( is_resource($$link) ) {
		$_link = $$link;
	} elseif( is_object($adb) && $adb->_link ) {
		$_link = $adb->_link;
	}
	
	return $_link;
}

function func_db_output($string) {
	if( is_string($string) ) {
		return htmlspecialchars($string);
	} elseif( is_array($string) ) {
		reset($string);
		
		while( list($key, $value) = each($string) ) {
			$string[$key] = func_db_output($value, $link);
		}
		return $string;
	} else {
		return $string;
	}
}

function func_db_input($string, $link = '_db_link') {
	$_link = func_db_link($link);
	if( is_string($string) ) {
		if( function_exists('mysql_real_escape_string') && is_resource($_link) ) {
			return mysql_real_escape_string($string, $_link);
		} elseif( function_exists('mysql_escape_string') ) {
			return mysql_escape_string($string);
		}
		
		return addslashes($string);
	} elseif( is_array($string) ) {
		reset($string);
		
		while( list($key, $value) = each($string) ) {
			$string[$key] = func_db_input($value, $link);
		}
		return $string;
	} else {
		return $string;
	}
}

function func_db_prepare_input($string) {
	if( is_string($string) ) {
		return trim(stripslashes($string));
	} elseif( is_array($string) ) {
		reset($string);
		
		while( list($key, $value) = each($string) ) {
			$string[$key] = func_db_prepare_input($value);
		}
		return $string;
	} else {
		return $string;
	}
}

//
function func_db_connect($server = CONFIG_DB_HOST, $username = CONFIG_DB_USER, $password = CONFIG_DB_PASSWORD, $database = CONFIG_DB_NAME, $link = '_db_link') {
	global $$link;
	
	if( CONFIG_DB_PCONNECT == 'true' ) {
		$$link = mysql_pconnect($server, $username, $password) or die('DBECP');
	} else {
		$$link = mysql_connect($server, $username, $password) or die('DBEC');
	}
	
	if( $$link ) {
		mysql_select_db($database) or die('DBEDB');
	}
	
	return $$link;
}

function func_db_close($link = '_db_link') {
	$_link = func_db_link($link);
	
	return mysql_close($_link);
}

function func_db_error($query, $errno, $error) { 
	die('<font color="#000000"><b>' . $errno . ' - ' . $error . '<br><br>' . $query . '<br><br><small><font color="#ff0000">[TEP STOP]</font></small><br><br></b></font>');
}

function func_db_query($query) {
	global $adb;
	
	//if( defined('CONFIG_DB_LOGSQL') && (CONFIG_DB_LOGSQL == 'true') ) {
	//	error_log('QUERY ' . $query . "\n", 3, CONFIG_DB_LOGFILE);
	//}
	
	$result = $adb->query($query);
	
	//if( defined('CONFIG_DB_LOGSQL') && (CONFIG_DB_LOGSQL == 'true') ) {
	//	error_log('RESULT ' . $result . ' ' . mysql_errno() . ' ' . mysql_error() . "\n", 3, CONFIG_DB_LOGFILE);
	//}
	
	return $result;
}

function func_db_perform($table, $data, $action = 'insert', $parameters = '', $link = '_db_link') {
	reset($data);
	
	if( $action == 'insert' ) {
		$query = 'insert into ' . $table . ' (';
		while( list($columns, ) = each($data) ) {
			$query .= $columns . ', ';
		}
		$query = substr($query, 0, -2) . ') values (';
		reset($data);
		while( list(, $value) = each($data) ) {
			switch((string)$value) {
				case 'now()':
					$query .= 'now(), ';
					break;
				case 'null':
					$query .= 'null, ';
					break;
				default:
					$query .= '\'' . func_db_input($value) . '\', ';
					break;
			}
		}
		$query = substr($query, 0, -2) . ')';
	} elseif( $action == 'update' ) {
		$query = 'update ' . $table . ' set ';
		while( list($columns, $value) = each($data) ) {
			switch((string)$value) {
				case 'now()':
					$query .= $columns . ' = now(), ';
					break;
				case 'null':
					$query .= $columns .= ' = null, ';
					break;
				default:
					$query .= $columns . ' = \'' . func_db_input($value) . '\', ';
					break;
			}
		}
		$query = substr($query, 0, -2) . ' where ' . $parameters;
	}
	
	return func_db_query($query, $link);
}

function func_db_fetch_array($db_query) {
	return mysql_fetch_array($db_query, MYSQL_ASSOC);
}

function func_db_num_rows($db_query) {
	return mysql_num_rows($db_query);
}

function func_db_data_seek($db_query, $row_number) {
	return mysql_data_seek($db_query, $row_number);
}

function func_db_insert_id() {
	return mysql_insert_id();
}

function func_db_free_result($db_query) {
	return mysql_free_result($db_query);
}

function func_db_fetch_fields($db_query) {
	return mysql_fetch_field($db_query);
}

//
function func_db_get_query_count() {
	global $adb;
	if( is_object($adb) ) {
		$count = $adb->getQueryCount();
		$query_count_result = '<div id="querycount">Query Count: ' . $count . '</div>';
	} else {
		$query_count_result = '<div id="querycount">No Query</div>';
	}
	
	return $query_count_result;
}
//
?>